﻿using System;
using System.Collections.Generic;
using System.Net;

namespace Toybox.NicoSharp.Video.Primitives
{
	using Utilities;

	partial class AbstractPacketCreator
	{
		public class ThreadKey : AbstractVideoKey
		{

			#region Constructor

			public ThreadKey(string thread, CookieCollection cookies)
				: base(thread, cookies)
			{
			}

			#endregion Constructor


			#region Public Members

			public bool Force184
			{
				get { return this._force184; }
				protected set
				{
					if (this._force184 == value) return;

					this._force184 = value;

					this.OnPropertyChanged("Force184");
				}
			}
			private bool _force184;

			#endregion Public Members


			#region Public Methods

			public string Update()
			{
				return base.Update(CreateUrl());
			}

			public void UpdateAsync()
			{
				base.UpdateAsync(CreateUrl());
			}

			protected virtual string CreateUrl()
			{
				Dictionary<string, string> dict = new Dictionary<string, string>();
				dict.Add("language_id", this.LanguageId.ToString());
				dict.Add("thread", this.Thread);

				return NicoUrls.FlApi + "getthreadkey?" + Serializer.AndEqualPairSerializer(dict);
			}

			#endregion Public Methods

			#region Override Methods

			protected override void SetKey(string text)
			{
				foreach (var kvp in Deserializer.AndEqualPairDeserializer(text))
				{
					switch (kvp.Key)
					{
						case "threadkey":
							this.Value = kvp.Value;
							break;
						case "force_184":
							this.Force184 = kvp.Value != "0";
							break;
					}
				}

				string[] values = Value.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
				Expires = int.Parse(values[0]);
			}

			#endregion Override Methods

		}
	}
}
